package com.codingman.data.mybatis.support;

import org.apache.ibatis.builder.annotation.ProviderContext;
import org.apache.ibatis.builder.annotation.ProviderMethodResolver;
import org.apache.ibatis.jdbc.SQL;

public class DeleteSqlProvider extends BaseSqlProviderSupport implements ProviderMethodResolver{
	
	public String deleteById(ProviderContext context) {
		Class<?> entityClass = getEntityClass(context);
		TableInfo table = TableInfo.forClass(entityClass);
		return new SQL().DELETE_FROM(table.getName())
				.WHERE(table.getPKConlumn() + " = #{id}")
				.toString();
	}
	
	public String deleteAll(ProviderContext context) {
		Class<?> entityClass = getEntityClass(context);
		TableInfo table = TableInfo.forClass(entityClass);
		return new SQL().DELETE_FROM(table.getName())
				.toString();
	}
	
	public <T> String delete(T entity) {
		Class<?> entityClass = entity.getClass();
		TableInfo table = TableInfo.forClass(entityClass);
		return new SQL().DELETE_FROM(table.getName())
				.WHERE(table.getPKConlumn() + " = #{" + table.getPKConlumn() + "}")
				.toString();
	}
}
